//
//  OperateWayTableViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/11/6.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "OperateWayTableViewController.h"
#import "OperationInfoBean.h"
#import "IQActionSheetPickerView.h"
#import "CommonDefine.h"

@interface OperateWayTableViewController ()<IQActionSheetPickerViewDelegate>
{
    NSInteger last_optype_index;
}
@property(strong, nonatomic)NSArray *optype_arr;//手术类型(器官类型)
@property(strong, nonatomic)NSArray *opway_arr;//手术方式
@property(strong, nonatomic)NSArray *identical_arr;//吻合方式
@property(strong, nonatomic)NSArray *identical_name_arr;//吻合方式名称数组
@end

@implementation OperateWayTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"手术方式";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction)];
    UIImage *leftImage=[[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftButtonItem=[[UIBarButtonItem alloc]initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem=leftButtonItem;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];//隐藏多余空cell
    
    
    
    if (self.opway.identical_type.length>0) {
        self.opway.identicalTypes = [[self.opway.identical_type componentsSeparatedByString:@","] mutableCopy];
        for (int i=0; i<self.opway.identicalTypes.count; i++) {
            NSString *iden = self.opway.identicalTypes[i];
            [self.opway.identicalTypes replaceObjectAtIndex:i withObject:[NSNumber numberWithInt:[iden integerValue]]];
        }
    }
    last_optype_index = -1;
    if (!self.opway.operation_type) {
        self.opway.operation_way = -1;
    }
    else {
        if ([self.optype_arr containsObject:self.opway.operation_type]) {
            last_optype_index = [self.optype_arr indexOfObject:self.opway.operation_type];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 业务逻辑
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)saveAction{
    self.opway.identical_type = [self.opway.identicalTypes componentsJoinedByString:@","];
    
    if ([self.delegate respondsToSelector:@selector(backOperationData:)]) {
        [self.delegate backOperationData:self.opway];
    }
    [self back];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = 0;
    if (!self.opway.operation_type) {
        count = 1;
    }
    else {
        NSUInteger index = [self.optype_arr indexOfObject:self.opway.operation_type];
        NSArray *identi = self.identical_arr[index];
        if (index==0) {
            count = 2 + identi.count;
        }
        else if (index==1){
            count = 1 + identi.count;
        }
        else
            count = 1;
    }
    
    return count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font=[UIFont systemFontOfSize:15];
        cell.detailTextLabel.font=[UIFont systemFontOfSize:15];
        cell.detailTextLabel.textColor=COLOR(180, 180, 180);
    }
    NSUInteger index = [self.optype_arr indexOfObject:self.opway.operation_type];
    if (indexPath.row==0) {
        cell.textLabel.text = @"手术类型";
        cell.detailTextLabel.text = self.opway.operation_type?self.opway.operation_type:@"";
    }
    else {
        if (index==NSNotFound) {
            cell.textLabel.text = @"手术方式";
        }
        else {
            NSArray *identi_arr_t = self.identical_arr[index];
            if (index==0) {
                if (indexPath.row==1) {
                    cell.textLabel.text = @"手术方式";
                    NSArray *opway_arr_t = self.opway_arr[index];
                    if (self.opway.operation_way<0) {
                        cell.detailTextLabel.text = @" ";
                    }
                    else {
                        NSInteger opway_index = self.opway.operation_way==0?0:self.opway.operation_way-1;
                        cell.detailTextLabel.text = opway_arr_t[opway_index];
                    }
                }
                else if (indexPath.row==2){
                    cell.textLabel.text = self.identical_name_arr[index][0];
                    if (self.opway.identicalTypes.count>0) {
                        NSInteger identi_index = [self.opway.identicalTypes[0] integerValue] - 1;
                        cell.detailTextLabel.text = identi_arr_t[0][identi_index];
                    }
                }
            }
            else if(index==1){
                cell.textLabel.text = self.identical_name_arr[index][indexPath.row-1];
                if (self.opway.identicalTypes.count>indexPath.row-1) {
                    NSInteger identi_index = [self.opway.identicalTypes[indexPath.row-1] integerValue] - 1;
                    cell.detailTextLabel.text = identi_arr_t[indexPath.row-1][identi_index];
                }
                else {
                    cell.detailTextLabel.text = @" ";
                }
            }
        }
        
    }
    
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger item_index = ROWINDEX(indexPath);
    NSArray *title_arr = nil;
    switch (indexPath.row) {
        case 0:{
            title_arr = self.optype_arr;
        }
            break;
        default:{
            NSUInteger optype_index = [self.optype_arr indexOfObject:self.opway.operation_type]; //移植(器官)类型索引
            if (optype_index==0) { //肝移植
                switch (indexPath.row) {
                    case 1:{
                        title_arr = self.opway_arr[0];
                    }
                        break;
                    case 2:{
                        title_arr = self.identical_arr[0][0];
                    }
                    default:
                        break;
                }
                
            }
            else if (optype_index==1){
                title_arr = self.identical_arr[optype_index][indexPath.row-1];
            }
        }
            break;
    }
    IQActionSheetPickerView *picker = [[IQActionSheetPickerView alloc] initWithTitle:@"" delegate:self];
    picker.titleFont = [UIFont systemFontOfSize:12];
    picker.titleColor = [UIColor redColor];
    [picker setTag:item_index];
    [picker setTitlesForComponents:@[title_arr]];
    [picker show];
}

#pragma mark - IQActionSheetPickerViewDelegate
-(void)actionSheetPickerView:(IQActionSheetPickerView *)pickerView didSelectTitles:(NSArray *)titles{
    NSIndexPath *ip = DEROWINDEX(pickerView.tag);
    NSString *valStr = [titles componentsJoinedByString:@""];
    
    if (ip.row==0) {
        NSUInteger optype_index = [self.optype_arr indexOfObject:valStr];
        if (last_optype_index!=optype_index) {
            [self.opway.identicalTypes removeAllObjects];
            if (optype_index!=0) {
                self.opway.operation_way = 0;
            }
        }
        last_optype_index = optype_index;
        self.opway.operation_type = valStr;
    }
    else {
        if (last_optype_index==0) { //肝移植
            if (ip.row==1) {
                NSArray *sub_opway_arr = self.opway_arr[last_optype_index];
                if ([sub_opway_arr containsObject:valStr]) {
                    self.opway.operation_way = [sub_opway_arr indexOfObject:valStr] + 1;
                }
            }
            else if (ip.row==2){
                NSArray *sub_identical_arr = self.identical_arr[last_optype_index][0];
                if ([sub_identical_arr containsObject:valStr]) {
                    NSUInteger id_index = [sub_identical_arr indexOfObject:valStr];
                    if (self.opway.identicalTypes.count==0) {
                        [self.opway.identicalTypes addObject:[NSNumber numberWithInt:(id_index+1)]];
                    } else {
                        [self.opway.identicalTypes replaceObjectAtIndex:(ip.row-2) withObject:[NSNumber numberWithInt:(id_index+1)]];
                    }
                }
            }
        }
        else if (last_optype_index==1){ //肾移植
            NSArray *sub_identical_arr = self.identical_arr[last_optype_index][ip.row-1];
            if ([sub_identical_arr containsObject:valStr]) {
                NSUInteger id_index = [sub_identical_arr indexOfObject:valStr];
                if (self.opway.identicalTypes.count<ip.row) {
                    [self.opway.identicalTypes addObject:[NSNumber numberWithInt:(id_index+1)]];
                }
                else {
                    [self.opway.identicalTypes replaceObjectAtIndex:(ip.row-1) withObject:[NSNumber numberWithInt:(id_index+1)]];
                }
            }
        }
        else {
            if (ip.row>0) {
                NSLog(@"手术方式数据出现错误！");
            }
        }
    }
    
    [self.tableView reloadData];
}


#pragma mark - 数据配置
- (NSArray *)optype_arr{
    if (!_optype_arr) {
        _optype_arr = @[@"肝移植", @"肾移植", @"肺移植", @"心脏移植", @"胰腺移植", @"小肠移植", @"其它移植"];
    }
    return _optype_arr;
}

- (NSArray *)opway_arr{
    if (!_opway_arr) {
        _opway_arr = @[@[@"经典式", @"背驮式", @"改良背驮式", @"劈离肝移植"],
                       @[], @[], @[], @[], @[], @[]];
    }
    return _opway_arr;
}

- (NSArray *)identical_arr{
    if (!_identical_arr) {
        _identical_arr = @[ @[ @[@"端端吻合", @"胆肠吻合", @"端端吻合放T管"] ],
                           @[ @[@"髂外动脉", @"髂内动脉"], @[@"髂外静脉", @"其它"], @[@"吻合至膀胱", @"其它"] ],
                            @[],@[],@[],@[],@[]];
    }
    return _identical_arr;
}

- (NSArray *)identical_name_arr{
    if (!_identical_name_arr) {
        _identical_name_arr = @[ @[@"胆道吻合方式"],
                                 @[@"肾动脉吻合方式", @"肾静脉吻合方式", @"输尿管吻合方式"],
                                 @[],@[],@[],@[],@[]];
    }
    return _identical_name_arr;
}


- (OperationOperandiDTO *)opway{
    if (!_opway) {
        _opway = [OperationOperandiDTO new];
    }
    return _opway;
}


@end
